<?php
namespace app\index\controller;

use app\common\model\ZhSupport as ZhSupportModel;
use app\common\model\ZhSupportEducation as ZhSupportEducationModel;
use app\common\model\ZhSupportDoubt as ZhSupportDoubtModel;
use app\common\model\ZhSupportReply as ZhSupportReplyModel;
use app\common\model\ZhSupportReply;

/**
 * 答疑解惑控制器
 * Class Zhsupport
 * @package app\index\controller
 */
class Zhsupport extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        $emodel = new ZhSupportEducationModel;
        $dmodel = new ZhSupportDoubtModel;
        $rmodel = new ZhSupportReplyModel;
        $this->getPageAndSize($param);
        $map = [];
        $user = $this->user;
        try {
            if(!empty($param['con'])){
                $map['s.content|m.username'] = $param['con'];
            }
            if(!empty($param['start']) && !empty($param['end'])){
                $map['create_time'] = ['between',[strtotime($param['start']),strtotime($param['end'])]];
            }
            if(!empty($param['is_reply'])){
                $map['s.is_reply'] = $param['is_reply'];
            }
            $map['s.comid'] = $user['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size,'s.id,s.comid,s.content,s.reply,s.is_reply,s.create_time,s.update_time,m.username,m.img,m.sex');
            foreach($lists as &$v){
                $http = substr($v['img'],0,4);
                if($http != 'http'){
                    $v['img'] = config('domain_name').$v['img'];
                }
                $v['education_num'] = $emodel->getCount(['s.comid'=>$v['comid'],'d.sid'=>$v['id']]);
                $v['doubt_num'] = $dmodel->getCount(['s.comid'=>$v['comid'],'d.sid'=>$v['id']]);
                $v['reply_num'] = $rmodel->getCount(['s.sid'=>$v['id']]);
            }
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [edit  编辑]
     * @return json
     * @author [C。]
     */
    public function edit()
    {
        $param = input('param.');
        $model = new ZhSupportModel;
        try{
            $param['is_reply'] = 1;
            $model->editData($param);
        }catch(\Exception $e){
            systembug($e->getMessage(),3,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '回复成功');
    }
}